package com.xingdata.jjxc.activity;

import jar.MessageManager;
import jar.listener.CaptureListener;
import jar.listener.UpgradeListener;
import jar.model.RequestEntity;
import jar.observer.CommunicationObserver;
import jar.tools.InstructionSet;
import jar.tools.UdpTools;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.HudVersionEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.VoideEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.SystemInfo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity implements OnClickListener,
		UpgradeListener {
	private final String TAG = "HUDUpgradeActivity";
	private TextView hud_version;
	private HashMap<String, String> paramMap = new HashMap<String, String>();
	private HudVersionEntity versionEntity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hud_upgrade);
		initView();
		initData();

	}

	private void initData() {
		// TODO Auto-generated method stub
		if (UdpTools.getHudVersion() != null) {
			hud_version.setText(UdpTools.getHudVersion());
		}
	}

	private void initView() {
		hud_version = (TextView) findViewById(R.id.hud_version);
		((Button) findViewById(R.id.download)).setOnClickListener(this);
		((Button) findViewById(R.id.Getversion)).setOnClickListener(this);
		((Button) findViewById(R.id.Upgrade_button)).setOnClickListener(this);
		// 初始化监听
		CommunicationObserver.addListener(new CaptureListener() {
			@Override
			public void onCapture(String msg) {
				System.out.println(msg);
			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.download:
			HttpUtil.getInstance(this).downloadFile(
					versionEntity.getVersion_path(),
					SDCardTools.getFilePath(SDCardTools.UPGRADEDATA_NAME)
							+ "/"
							+ SDCardTools.getFileName(versionEntity
									.getVersion_path()),
					new HttpResponseListener() {
						@Override
						public void onSuccess(RespEntity respEntity) {
							Log.i(TAG, "下载完成");

						}

						@Override
						public void onLoading(int vurrent) {
							// TODO Auto-generated method stub
							System.out.println("进度" + vurrent);
						}

						@Override
						public void onFailure(String result) {
							// TODO Auto-generated method stub

						}
					});

			break;
		case R.id.Getversion:

			if (paramMap.size() != 0) {
				paramMap.clear();
			}
			paramMap.put("version", UdpTools.getHudVersion());
			HttpUtil.getInstance(this).Post(App.ZZD_REQUEST_GETHUDVERSION,
					paramMap, new HttpResponseListener() {

						@Override
						public void onSuccess(RespEntity respEntity) {
							versionEntity = JSON.parseObject(
									respEntity.getResult(),
									HudVersionEntity.class);
						}

						@Override
						public void onLoading(int vurrent) {

						}

						@Override
						public void onFailure(String result) {

						}
					});
			break;

		case R.id.Upgrade_button:
			if (versionEntity == null) {
				return;
			}
			JSONObject jsonObject = new JSONObject();
			// IP地址+端口号+文件夹名称+文件名称
			versionEntity.setVersion_path("http://" + UdpTools.getPhoneIP()
					+ ":" + SystemInfo.getServerPORT() + "/"
					+ SDCardTools.UPGRADEDATA_NAME + "/"
					+ SDCardTools.getFileName(versionEntity.getVersion_path()));
			jsonObject = (JSONObject) jsonObject.toJSON(versionEntity);
			MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()),
					new RequestEntity(jsonObject.toJSONString(),
							InstructionSet.INSTRUCTION_AUTH_GET_UPGRADE_TO_HUD
									+ ""));
			break;
		default:
			break;
		}
	}

	@Override
	public void downloadState(int state) {
		Log.i(TAG, "下载状态" + state);
	}

	public void onTest(View view) {

		MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()),
				new RequestEntity("",
						InstructionSet.INSTRUCTION_AUTH_CAPTURE_TO_HUD + ""));

		// //请求视频列表
		// HttpUtil.getInstance(this).requestByHttpGet(
		// "http://192.168.168.101:8999/hudimeye/.json",
		// new HttpResponseListener() {
		//
		// @Override
		// public void onSuccess(RespEntity respEntity) {
		// String msg = respEntity.getMsg();
		// List<VoideEntity> entities =JSON.parseArray(msg, VoideEntity.class);
		// System.out.println("Aaa");
		// }
		//
		// @Override
		// public void onLoading(int vurrent) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void onFailure(String result) {
		//
		// }
		// });
		//
	}

}
